import { defineStore } from 'pinia'
export const useWebConfigStore = defineStore('webConfig', {
  state: () => {
    return localStorage.getItem('b2cWebConfigData') ? JSON.parse(localStorage.getItem('b2cWebConfigData')) : {
      bsLoginLogoImg: null,
      bsLoginBgImg: null,
      bsCopyrightCn: null,
      bsCopyrightEn: null,
      bsTitleContentCn: null,
      bsTitleContentEn: null,
      bsTitleImg: null,
      bsMenuTitleOpenCn: null,
      bsMenuTitleOpenEn: null,
      bsMenuTitleCloseCn: null,
      bsMenuTitleCloseEn: null
    }
  },
  actions: {
    addData (webConfigDataForm) {
      this.$patch(() => {
        localStorage.setItem('b2cWebConfigData', JSON.stringify(webConfigDataForm || '{}'))
        const lang = localStorage.getItem('b2cLang')
        if (lang !== 'en') {
          document.title = webConfigDataForm.bsTitleContentCn || ''
        } else {
          document.title = webConfigDataForm.bsTitleContentEn || ''
        }
        let facicon = document.querySelector('link[rel="icon"]')
        if (facicon !== null) {
          facicon.href = webConfigDataForm.bsTitleImg
        } else {
          facicon = document.createElement('link')
          facicon.rel = 'icon'
          facicon.href = webConfigDataForm.bsTitleImg
          document.head.appendChild(facicon)
        }
      })
    }
  }
})
